/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.ems.service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.constant.Constant;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.ems.dao.EmsCartDao;
import com.thinkgem.jeesite.modules.ems.entity.EmsCart;
import com.thinkgem.jeesite.modules.ems.entity.EmsCartHis;
import com.thinkgem.jeesite.modules.sys.utils.DictUtils;

/**
 * 购物车Service
 * @author zad
 * @version 2018-01-25
 */
@Service
@Transactional(readOnly = true)
public class EmsCartService extends CrudService<EmsCartDao, EmsCart> {

	public EmsCart get(String id) {
		return super.get(id);
	}
	
	public List<EmsCart> findList(EmsCart emsCart) {
		return super.findList(emsCart);
	}
	
	public Page<EmsCart> findPage(Page<EmsCart> page, EmsCart emsCart) {
		return super.findPage(page, emsCart);
	}
	
	@Transactional(readOnly = false)
	public void save(EmsCart emsCart) {
		super.save(emsCart);
	}
	
	@Transactional(readOnly = false)
	public void delete(EmsCart emsCart) {
		super.delete(emsCart);
	}
	
	// 查询购物车中该用户的商品数
	public Integer countCart(String userid)
	{
		return dao.countCart(userid);
	}
	
	/**
	 *  购物车信息分析统计
	 * @param carts
	 * @param discountType 1：平均分配；2：按比例分配
	 * @return
	 */
	
	public Map<String,String> analysisCartInfo(List<EmsCart> carts, String discountType)
	{
		if(null == carts || carts.size() < 1)
		{
			return null;
		}
		// 返回结果
		Map<String,String> map = new HashMap<String,String>();
		// 计算人数
		Set<String> personCountSet = new HashSet<String>();
		// 课程原价合计
		BigDecimal totalPriceFee = new BigDecimal(0);
		
		// 记录每人所选课程数量
		Map<String,Integer> courseCountMap = new HashMap<String,Integer>();
		for(EmsCart cart:carts)
		{
			BigDecimal fee = new BigDecimal(0);
			if(null != cart.getCourse() && null != cart.getCourse().getFee())
			{
				fee = cart.getCourse().getFee();
			}
			totalPriceFee = totalPriceFee.add(fee);
			personCountSet.add(cart.getStudent().getId());
			
			String studentId = cart.getStudent().getId();
			if(!courseCountMap.containsKey(studentId))
			{
				courseCountMap.put(studentId,1);
			}
			else
			{
				courseCountMap.put(studentId,courseCountMap.get(studentId) + 1);
			}
		}
		// 报名人数
		map.put("personNum", String.valueOf(personCountSet.size()));
		// 报名人次
		map.put("personTime", String.valueOf(carts.size()));
		// 总报名价格
		map.put("totalPriceFee", String.valueOf(totalPriceFee.setScale(2)));
				
		// 记录每个学员的优惠券价格(根据每人所选课程数量计算)
		Map<String,BigDecimal> couponMoneyMap = new HashMap<String,BigDecimal>();
		
		// 根据每个人的报班次数来计算优惠券价格
		if(null != courseCountMap && courseCountMap.entrySet().size() > 0)
		{
			for(Map.Entry<String, Integer> entry:courseCountMap.entrySet())
			{
				String stuId = entry.getKey();
				Integer count = entry.getValue();
				// 得到优惠总价
				String couponMoney = DictUtils.getDictLabel(String.valueOf(count), "ems_join_coupon", 
						DictUtils.getDictLabel(String.valueOf(count+"-"), "ems_join_coupon", 
						DictUtils.getDictLabelEndWith(String.valueOf("-"), "ems_join_coupon", 
						DictUtils.getDictLabelMax("ems_join_coupon", "0"))));
				couponMoneyMap.put(stuId, new BigDecimal(couponMoney));
			}
		}
		// 按平均计算
		if(discountType.equals(Constant.Dict.DICT_DISCOUNT_TYPE_SHARE))
		{
			// 优惠后总价
			BigDecimal totalRealFee = new BigDecimal(0);
			for(EmsCart cart:carts)
			{
				String stuId = cart.getStudent().getId();
				BigDecimal fee = cart.getCourse().getFee();
				BigDecimal siteDiscount = (null == cart.getSiteDiscount()) ? new BigDecimal(1):cart.getSiteDiscount();
				BigDecimal realMoney = fee.multiply(siteDiscount).subtract(couponMoneyMap.get(stuId).divide(new BigDecimal(courseCountMap.get(stuId)))).setScale(2);
				cart.setPayMoney(realMoney);
				totalRealFee = totalRealFee.add(realMoney);
			}
			map.put("totalRealFee",String.valueOf(totalRealFee));
		}
		
		// 按比例计算
		if(discountType.equals(Constant.Dict.DICT_DISCOUNT_TYPE_SHARE))
		{
			// 需要时再实现 2018-02-05
		}
		
		
		return map;
	}
	
	// 根据创建人删除购物车
	public int deleteByCreate(EmsCart emsCart)
	{
		return dao.deleteByCreate(emsCart);
	}
	
	// 校验学员是否存在 add by zad 2018-02-23
	public List<EmsCart> checkExists(EmsCart emsCart)
	{
		return dao.checkExists(emsCart);
	}
}